package com.yq.servlet;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import sun.security.x509.IPAddressName;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.event.ItemEvent;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //判断请求中是否包含文件类型
//        boolean multipartContent = ServletFileUpload.isMultipartContent(request);
//        if (!multipartContent){
//            return;
//        }
//        //设置缓存的目录
//        DiskFileItemFactory factory = new DiskFileItemFactory();
//        ServletContext servletContext = getServletContext();
//        File respository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
//        factory.setRepository(respository);
//
//        //处理上传文件的处理器
//        ServletFileUpload upload = new ServletFileUpload(factory);
//        //上传文件名中文乱码问题
//        upload.setHeaderEncoding("utf-8");
//        //上传文件大小的限制
//        upload.setFileSizeMax(100000);
//
//        try {
//            List<FileItem> fileItems = upload.parseRequest(request);
//            for (FileItem fileItem : fileItems) {
//                //如果是表单的数据
//                if (fileItem.isFormField()){
//                    processFormFiled(fileItem);
//                }else{
//                    //上传的文件
//                    processFile(fileItem);
//                }
//            }
//        } catch (FileUploadException e) {
//            e.printStackTrace();
//        }
//
//
//    }
//
//    private void processFile(FileItem fileItem) {
//        String fieldName = fileItem.getFieldName();
//        String name = fileItem.getName();
//        String contentType = fileItem.getContentType();
//        boolean inMemory = fileItem.isInMemory();
//        long size = fileItem.getSize();
//        //文件的保存
//        String realPath = getServletContext().getRealPath("image/" + name);
//        File file = new File(realPath);
//        if (!file.getParentFile().exists()){
//            file.getParentFile().mkdirs();
//        }
//        if (file.exists()){
//            file.delete();
//        }
//        try {
//            fileItem.write(file);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//    }
//
//    private void processFormFiled(FileItem fileItem) {
//
//        String name = fileItem.getFieldName();
//        String string = null;
//        try {
//            string = fileItem.getString("utf-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        System.out.println(name+":"+string);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
